package com.sbhotel.chotellauncher.widget;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.sbhotel.chotellauncher.R;
import com.sbhotel.chotellauncher.constant.Constants;
import com.sbhotel.chotellauncher.util.SPUtil;
import com.sbhotel.chotellauncher.util.Utils;

public class PowerOffDialog extends Dialog implements View.OnFocusChangeListener, View.OnClickListener {

    Context mContext;
    View powerOff;
    ImageView cancel;
    TextView tips, countDown;
    int countDownNum = 15;

    public PowerOffDialog(@NonNull Context context) {
        super(context);
        mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_power_off);

        initView();
        //initData();

        //按空白处不能取消动画
        setCanceledOnTouchOutside(true);
        //设置window背景，默认的背景会有Padding值，不能全屏。当然不一定要是透明，你可以设置其他背景，替换默认的背景即可。
        getWindow().setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(mContext, R.color.black_trans_90)));
        //一定要在setContentView之后调用，否则无效
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

        handler.sendEmptyMessageDelayed(1, 1000);
    }

    public void initView() {
        powerOff = findViewById(R.id.power_off_layout);
        powerOff.setOnFocusChangeListener(this);
        powerOff.setOnClickListener(this);
        cancel = findViewById(R.id.cancel_layout);
        cancel.setOnFocusChangeListener(this);
        cancel.setOnClickListener(this);
        tips = findViewById(R.id.tips);
        countDown = findViewById(R.id.tv_count_down);
    }

    @Override
    public void onFocusChange(View view, boolean b) {
        switch (view.getId()) {
            case R.id.power_off_layout:
                powerOff.setBackground(ContextCompat.getDrawable(mContext, R.mipmap.icon_shutdown_active));
                cancel.setImageResource(R.mipmap.icon_cancel);
                tips.setText(mContext.getText(R.string.tips_power_off));
                break;
            case R.id.cancel_layout:
                powerOff.setBackground(ContextCompat.getDrawable(mContext, R.mipmap.icon_shutdown));
                cancel.setImageResource(R.mipmap.icon_cancel_active);
                tips.setText(mContext.getText(R.string.tips_cancel));
                break;
            default:
                break;
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.power_off_layout:
                Utils.shutDownDevice(mContext);
                break;
            case R.id.cancel_layout:
                if(handler.hasMessages(1)) {
                    handler.removeMessages(1);
                    countDownNum = 15;
                }
                SPUtil.put(mContext, Constants.SP_KEY_SLEEP_VALUE, "");
                dismiss();
                break;
            default:
                break;
        }
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    countDownNum--;
                    if(countDownNum <= 0) {
                        Utils.shutDownDevice(mContext);
                    } else {
                        countDown.setText(countDownNum + "s");
                        handler.sendEmptyMessageDelayed(1, 1000);
                    }
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if(handler.hasMessages(1)) {
            handler.removeMessages(1);
            countDownNum = 15;
        }
        SPUtil.put(mContext, Constants.SP_KEY_SLEEP_VALUE, "");
    }

}
